//
//  FirstViewController.m
//  EducationLive
//
//  Created by Apple on 2017/9/26.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//

#import "HomeVC.h"
#import "LiveListVC.h"
#import "HomeTopView.h"
#import "HomeModel.h"

@interface HomeVC ()<TYTabPagerControllerDataSource, TYTabPagerControllerDelegate>
// 栏目数组
@property (nonatomic, strong) NSMutableArray *columnArray;

@end

@implementation HomeVC
#pragma mark - lifeCycle
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // 加载栏目样式
        [self configurePagerStyles];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        // 加载栏目样式
        [self configurePagerStyles];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 把视图扩展到底部tabbar
    self.edgesForExtendedLayout = UIRectEdgeBottom;
    // 加载数据
    [self loadData];
    // 加载栏目按钮样式
    [self configureTabButtonPager];
    // 加载顶部视图
    [self addTopView];
    
    self.view.backgroundColor = [UIColor redColor];
}
// 加载栏目样式
- (void)configurePagerStyles
{
    self.tabBarHeight = 40;
}
// 加载栏目按钮样式
- (void)configureTabButtonPager
{
    self.dataSource = self;
    self.delegate = self;
    self.tabBar.layout.barStyle = TYPagerBarStyleProgressElasticView;
    self.tabBar.layout.normalTextFont = [UIFont systemFontOfSize:17];
    self.tabBar.layout.selectedTextFont = [UIFont systemFontOfSize:20];
    self.tabBar.backgroundColor = [UIColor whiteColor];
    self.tabBar.layout.progressColor = GreenColor;
    self.tabBar.layout.normalTextColor = RGB_255(51, 51, 51);
    self.tabBar.layout.selectedTextColor = GreenColor;
    self.tabBar.layout.sectionInset = UIEdgeInsetsMake(0, 12, 0, 12);
    self.tabBar.layout.cellSpacing = 10;
//    self.tabBar.layout.cellSpacing = (kScreenWidth - _newsPageInfos.count*self.tabBar.layout.cellWidth - 2*self.tabBar.layout.sectionInset.left)/(_newsPageInfos.count-1);
    self.tabBar.layout.cellEdging = 0;
    self.tabBar.layout.progressVerEdging = 3;
}
// 加载顶部视图
- (void)addTopView
{
    HomeTopView *homeTopView = [[HomeTopView alloc]init];
    homeTopView.frame = CGRectMake(0, 0, kScreenWidth, 44);
    homeTopView.backgroundColor = [UIColor clearColor];
    [homeTopView.messageBtn addTarget:self action:@selector(messageBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = homeTopView;
}

#pragma mark - topView Action
- (void)messageBtnAction:(UIButton *)messageBtn
{
    
}

#pragma mark - load data
// 加载数据
- (void)loadData
{
    __typeof (self) __weak weakSelf = self;
//    [NetworkState networkState:^(bool has) {
        // 栏目接口
        [MAAFRequest getColumn:nil networkState:NO successBlock:^(id responseBody) {
            //获取数据
            weakSelf.columnArray = [HomeModel arrayOfModelsFromDictionaries:Infos error:nil];
            // 刷新数据
            [weakSelf reloadData];
        } failureBlock:^(NSString *error) {
            
        }];
//    }];
}

#pragma mark - TYPagerControllerDataSource

- (NSInteger)numberOfControllersInTabPagerController
{
    return _columnArray.count;
}

- (NSString *)tabPagerController:(TYTabPagerController *)tabPagerController titleForIndex:(NSInteger)index
{
    HomeModel *homeModel = _columnArray[index];
    return homeModel.name ? homeModel.name : @"";
}

- (UIViewController *)tabPagerController:(TYTabPagerController *)tabPagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching
{
    HomeModel *homeModel = _columnArray[index];
    LiveListVC *newsVC = [[LiveListVC alloc]init];
    newsVC.cate_id = homeModel.cate_id;
    newsVC.index = index;
    // 扩展到底部tabbar
    newsVC.extendedTabBarInset = YES;
    return newsVC;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
